package cn.sany.cterminal.service.impl;

import cn.sany.cterminal.common.constants.CterminalConst;
import cn.sany.cterminal.dao.*;
import cn.sany.cterminal.entity.RealtimeInfo;
import cn.sany.cterminal.entity.VehicleInfo;
import cn.sany.cterminal.entity.mq.FaultInfoMsg;
import cn.sany.cterminal.entity.mq.RealtimeInfoMsg;
import cn.sany.cterminal.service.MqService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 *
 * @Project : forklift
 * @Package : cn.sany.forklift.service
 * @Class : MqService
 * @Description : 处理mq数据业务
 * @author : zhouxing
 * @CreateDate : 2020-07-18 11:29:21
 * @version : V0.5
 * @Copyright : 2020 sany.com Inc. All rights reserved.
 * @Reviewed :
 * @UpateLog :    Name    Date    Reason/Contents
 *             ---------------------------------------
 *                ****    ****    ****
 *
 */
@Service
@Slf4j
public class MqServiceImpl implements MqService{
    @Autowired(required = false)
    HistoryInfoDao historyInfoDao;
    @Autowired(required = false)
    VehicleInfoDao vehicleInfoDao;
    @Autowired(required = false)
    VehicleOperateRecordDao vehicleOperateRecordDao;
    @Autowired(required = false)
    RealtimeInfoDao realtimeInfoDao;
    @Override
    public void dealRealtimeInfo(RealtimeInfoMsg realtimeInfoMsg) {
        VehicleInfo vehicleInfo = vehicleInfoDao.selectByVinNum(realtimeInfoMsg.getVinNumber());
        if(vehicleInfo == null){
            log.info("车辆不存在，vinNum:{}",realtimeInfoMsg.getVinNumber());
        }
        if(vehicleInfo.getEnableState() != CterminalConst.EnableState.ABLE){
            log.info("车辆已禁用，vinNum:{}",realtimeInfoMsg.getVinNumber());
        }
        RealtimeInfo oldInfo = realtimeInfoDao.selectByVehicleId(vehicleInfo.getId());
        if(realtimeInfoMsg.getCollectTime().getTime() < oldInfo.getUpdateTime().getTime()){
            log.info("垃圾数据：{}，实时数据:{}",realtimeInfoMsg.toString(),oldInfo.toString());
        }
        RealtimeInfo newInfo = createRealtimeInfo( realtimeInfoMsg);
        newInfo.setVehicleId(vehicleInfo.getId());
        BeanUtils.copyProperties(oldInfo,newInfo);
        BeanUtils.copyProperties(realtimeInfoMsg,newInfo);
        newInfo.setUpdateTime(realtimeInfoMsg.getCollectTime());
        newInfo.setOnlineState(1);
        newInfo.setSpeed(realtimeInfoMsg.getSpeed().doubleValue());
        if(realtimeInfoMsg.getLatitude() != 0 && realtimeInfoMsg.getLongitude()!= 0 ){
            newInfo.setLatitude(realtimeInfoMsg.getLatitude()+"");
            newInfo.setLongitude(realtimeInfoMsg.getLongitude()+"");
        }
        newInfo.setDistance(realtimeInfoMsg.getDistance().doubleValue());
//        if(realtimeInfoMsg.getDistance() > oldInfo.getDistance() ){
//            newInfo.setDistance(realtimeInfoMsg.getDistance().doubleValue());
//        }
        if(realtimeInfoMsg.getSpeed().equals(0) &&
                oldInfo.getSpeed().equals(0) &&
                realtimeInfoMsg.getSurplusPower() <  oldInfo.getSurplusPower()){
            newInfo.setElectricityConsume(oldInfo.getElectricityConsume() + realtimeInfoMsg.getSurplusPower() * vehicleInfo.getElectricityRange());
        }
        if((realtimeInfoMsg.getCollectTime().getTime() - oldInfo.getUpdateTime().getTime()) < 10000){
            newInfo.setBootTime(oldInfo.getBootTime() + (realtimeInfoMsg.getCollectTime().getTime() - oldInfo.getUpdateTime().getTime()));
        }
        realtimeInfoDao.updateByVehicleId(newInfo);
    }

    private RealtimeInfo createRealtimeInfo(RealtimeInfoMsg realtimeInfoMsg){
        RealtimeInfo realtimeInfo = new RealtimeInfo();
        realtimeInfo.setDistance(realtimeInfoMsg.getDistance().doubleValue());
        realtimeInfo.setLatitude(realtimeInfoMsg.getLatitude()+"");
        realtimeInfo.setLongitude(realtimeInfoMsg.getLongitude()+"");
        if(realtimeInfoMsg.getSpeed().equals(0)){
            realtimeInfo.setSurplusPower(realtimeInfoMsg.getSurplusPower());
        }
        realtimeInfo.setDirection(realtimeInfoMsg.getDirection());
        realtimeInfo.setSpeed(realtimeInfoMsg.getSpeed().doubleValue());
        return realtimeInfo;
    }
    @Override
    public void dealFaultInfo(FaultInfoMsg faultInfoMsg) {
        VehicleInfo vehicleInfo = vehicleInfoDao.selectByVinNum(faultInfoMsg.getVinNum());
        RealtimeInfo oldInfo = realtimeInfoDao.selectByVehicleId(vehicleInfo.getId());
        if(vehicleInfo == null){
            log.info("故障信息系车辆不存在，vinNum:{}",faultInfoMsg.getVinNum());
        }
        if(vehicleInfo.getEnableState() != CterminalConst.EnableState.ABLE){
            log.info("故障信息系车辆不存在，vinNum:{}",faultInfoMsg.getVinNum());
        }
        RealtimeInfo realtimeInfo = new RealtimeInfo();
        realtimeInfo.setVehicleId(vehicleInfo.getId());
        //realtimeInfo.setUpdateTime(faultInfoMsg.getCollectTime());
        realtimeInfo.setFaultNumber(faultInfoMsg.getTotalNumber());
        realtimeInfoDao.updateByVehicleId(realtimeInfo);
    }
}
